//
//  ViolationModel.m
//  carcareIOS
//
//  Created by 王然 on 16/8/3.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "VioCarModel.h"
#import "CalendarMgr.h"
#import "MapGeocode.h"


@interface VioCarModel ()

@property (nonatomic, copy) NSDictionary *carinfo;
@property (nonatomic, copy) NSArray *cities;
@property (nonatomic, copy) NSArray *violations;

@end

@implementation VioCarModel

-(void)setCarinfo:(NSDictionary *)carinfo{
    _carinfo = [carinfo copy];
    self.carinfoModel = [[CarInfoModel alloc] initWithDic:carinfo];
}

-(void)setCities:(NSArray *)cities{
    _cities = cities;
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:cities.count];
    [cities enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        VioCityModel *model = [[VioCityModel alloc] initWithDic:obj];
        [tmp addObject:model];
    }];
    self.cityModels = [tmp copy];
}

-(void)setCityModels:(NSArray *)cityModels{
    _cityModels = cityModels;
    NSMutableString *show = [NSMutableString stringWithCapacity:3];
    NSMutableString *cIDs = [NSMutableString stringWithCapacity:3];
    __block BOOL needIDNum = NO;
    [cityModels enumerateObjectsUsingBlock:^(VioCityModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.needIdNum) needIDNum = YES;
        if (idx != 0) {
            [cIDs appendString:@","];
            if (idx < 2) {
                [show appendString:@","];
            }
        }
        [cIDs appendString:[NSString stringWithFormat:@"%zd",obj.cid]];
        if (idx < 2) {
            [show appendString:obj.name];
        }
    }];
    if (cityModels.count > 2) {
        [show appendString:@"等"];
    }
    self.needIDNum = needIDNum;
    self.showCitys = [show copy];
    self.carinfoModel.driveCity = cIDs;
}

-(BOOL)isVioInfoComplete{
    if (self.status == 0 && self.cityModels.count > 0 && self.carinfoModel.carno.length > 6 && self.carinfoModel.ecode.length > 3 && self.carinfoModel.vcode.length > 6) {
        return !self.needIDNum || (self.needIDNum && [self.carinfoModel.idnum isNotNil]);
    }else{
        return NO;
    }
}

-(void)setViolations:(NSArray *)violations{
    _violations = violations;
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:violations.count];
    [violations enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [tmp addObject:[[VioModel alloc] initWithDic:obj]];
    }];
    self.vioModels = [tmp copy];
}

@end


@implementation VioModel

-(void)setDate:(NSTimeInterval)date{
    _date = date;
    self.showTime = [CALENDARMGR dateStrWithDate:[NSDate dateWithTimeIntervalSince1970:date] format:yyyy_MM_dd_HH_mm];
}

-(void)setAddress:(NSString *)address{
    _address = [address copy];
    __weak __typeof(self) wself = self;
    [MapGeocode geoCodeAddress:address finish:^(NSArray<AMapGeocode *> *geocodes) {
        if(geocodes.count > 0)wself.coordinate = CLLocationCoordinate2DMake(geocodes[0].location.latitude, geocodes[0].location.longitude);
    } error:nil];
}

@end
